// 获取地址栏id
let id = new URLSearchParams(location.search).get('id');


/**
 * **************************ajax渲染评论区
 */
function getArticle() {
    $.ajax({
        url: `http://localhost:8888/api/articles/${id}/comments`,
        success: function(res) {
            if (res.status === 0) {
                let arr = ['<h4><i class="sprites"></i>评论区</h4>']
                res.data.forEach(item => {
                    arr.push(` <div class="kr_comment_card">
                    <div class="img-wrap">
                        <img src="./uploads/avatar_3.jpg" alt="">
                    </div>
                    <div class="info">
                        <p>${item.uname} · <span>${item.cdate}</span></p>
                        <p>${item.content}</p>
                    </div>
                    <a href="javascript:;" class="like" id='like_prise'>${item.count}</a>
                </div>`)
                });
                $('#kr').html(arr.join(''))
            } else {

                //如果没有评论
                $('#kr').html('暂时还没有评论哦,快来抢沙发吧')
                $('#kr').css({
                    color: '#FFB800'
                })
            }
        }
    })
}

getArticle()

// 获取评论
$('#btn_comments').on('click', function() {
    let content = $('#content').val()
    let uname = $('#uname').val()

    if (content === '' || uname === '') {
        layer.msg('请输入姓名和内容后评论')
    } else {
        $.ajax({
            type: 'post',
            url: `http://localhost:8888/api/articles/${id}/comments`,
            data: {
                content: content,
                uname: uname
            },
            success: function(res) {
                if (res.status === 0) {
                    layer.msg('评论成功')
                    $('#content').val('')
                    $('#uname').val('')
                    $('.word_count span').html(0)
                    getArticle()
                }
            }
        })
    }

})

// 评论字数长度
$('#content').on('keyup', function() {
    let val = $(this).val()
    $('.word_count span').html(val.length)
})

// 点赞效果
$('#kr').on('click', '#like_prise', function() {
    let prise = $(this).html()
    let priseNum = Number(prise) + 1
    $(this).html(priseNum)
    layer.msg('wink~~ 点赞成功', { icon: 6 });
}).on('dblclick', '#like_prise', function() {
    let prise = $(this).html()
    let priseNum = Number(prise) - 1
    $(this).html(priseNum)
    layer.msg('~~别点这么快嘛', { icon: 5 });
})

// 文章点赞
$('#data_like').on('click', function() {
    let prise = $(this).data('count')
    let priseNum = Number(prise) + 1
    $(this).attr('data-count', priseNum)
    layer.msg('谢谢您的鼓励')
})


// 返回顶部
$('.gotop').on('click', function() {
    $('html,body').animate({ 'scrollTop': '0' }, 500, )
}).on('mouseenter', function() {})

let timer = null

// 防抖
$(window).on('scroll', function() {
    clearTimeout(timer)
    timer = setTimeout(() => {
        if ($(this).scrollTop() > 2200 && $(this).scrollTop() < 2400) {
            layer.tips('返回顶部直通车', '.gotop');
        }
    }, 15)
})

$('#uname').on('mouseenter', function() {
    console.log(1);
})


/*************************************** */